<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class Activity extends Base
{
//前置操作定义
    protected $beforeActionList = [
        // 'second' =>  ['except'=>'hello'],
        'beforeEdit'  =>  ['only'=>'edit'],
    ];
    private $mod;
    private $_cate_mod;
    public function _initialize(){
        parent::_initialize();
        $this->mod = model('activity');
        $this->_cate_mod = model('activity_cate');
        $this->view_del = true;
        $this->list_field = 'id,cate_id,title,img,create_time,status,ordid';
    }

    public function _search(){
        $map = array();
        $time_start = input('get.time_start');
        $time_end = input('get.time_end');
        if($time_start && !$time_end){
            $map['create_time'] = ['egt',strtotime($time_start)];
        }elseif($time_end && $time_end){
            $map['create_time'] = ['between',[strtotime($time_start),strtotime($time_end)+86399]];
        }
        ($keyword = input('get.keyword')) && $map['title'] = ['like', '%'.$keyword.'%'];
        $cate_id = input('get.cate_id');
        $status = -1;
        if(isset($_GET['status']) && $_GET['status'] !== ''){
            $status = input('get.status/d');
        }
        $status>=0 && $map['status'] = ['eq',$status];
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = ['in', $id_arr];
            $spid = $this->_cate_mod->where(['id'=>$cate_id])->value('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }
    protected function beforeEdit(){
        $id = input('param.id/d');
        $article = $this->mod->withTrashed()->field('id,cate_id')->where(['id'=>$id])->find();
        $spid = $this->_cate_mod->where(['id'=>$article['cate_id']])->value('spid');
        if( $spid==0 ){
            $spid = $article['cate_id'];
        }else{
            $spid .= $article['cate_id'];
        }

        $this->assign('selected_ids',$spid);
    }
    //编辑 前置事件
    public function editDo(){
        \app\common\model\Activity::event('before_update',function($obj){
            //自动提取摘要
            if($obj->description == '' && isset($obj->info)) {
                $content = stripslashes($obj->info);
                $obj->description = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
                $obj->description = addslashes($obj->description);
            }
            $obj->cate_alias = $this->_cate_mod->getCateAlias($obj->cate_id);
        });
        parent::editDo();
    }
    //添加前置事件
    public function addDo(){
        \app\common\model\Activity::event('before_insert',function($obj){
            //自动提取摘要
            if($obj->description == '' && isset($obj->info)) {
                $content = stripslashes($obj->info);
                $obj->description = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
                $obj->description = addslashes($obj->description);
            }
            $obj->cate_alias = $this->_cate_mod->getCateAlias($obj->cate_id);
        });
        parent::addDo();
    }
    //回收站
    public function rollBack(){
        $this->_delAction = 'delTure';
        $this->_data = array(
            'recovery'    => array(
                'c' => 'Activity',
                'a' => 'recovery',
                'str'    => '<a data-acttype="ajax" data-uri="%s" data-msg="确定要恢复该条数据么？" class="J_confirmurl" href="javascript:;">恢复</a>',
                'param' => array('id'=>'@id@'),
                'isajax' => 1,
                'replace'=> ''
            )
        );
        $map = $this->_search();
        $list = $this->mod->onlyTrashed()->where($map)->order('delete_time desc,id desc')->paginate(20);//查询软删除数据
        $this->assign('options',$this->check());
        $this->assign('pages',$list->render());
        $this->assign('list',$list);
        $this->assign('list_table',true);
        return $this->fetch();
    }
    //真实删除
    public function delTure(){
        $this->del_true = true;
        parent::delete();
    }
    //恢复
    public function recovery(){
        $pk = $this->mod->getPk();
        $ids = trim(input($pk), ',');
        if ($ids) {
            $edit['delete_time'] = null;
            if($this->mod->onlyTrashed()->where([$pk=>['in',$ids]])->update($edit)){
                $this->ajaxReturn(1, '数据恢复成功');
            }else{echo $this->mod->getLastSql();
                $this->ajaxReturn(0, '数据恢复失败');
            }
        } else {
            $this->ajaxReturn(0, '参数错误');
        }
    }
}